package com.infoauto.opc.gateway.opc;

import com.infoauto.opc.gateway.messages.factory.RabbitMQFactory;
import com.infoauto.opc.gateway.messages.model.RabbitMQSender;
import com.infoauto.opc.gateway.opc.utils.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;

//接口信息处理
public class MessageHandler {
    @Autowired
    private SpringContextUtils springContextUtils;
    /// <summary>
    /// Manager实例
    /// </summary>
    private static MessageHandler _manager;
    /// <summary>
    /// 锁定实例
    /// </summary>
    private static Object oClassLock = new Object();
    /// <summary>
    /// 得到该实例
    /// </summary>
    /// <returns></returns>
    public static MessageHandler Instance()
    {
        synchronized (oClassLock) //加锁只生成一个实例
        {
            if (_manager == null)
            {
                _manager = new MessageHandler();
            }
        }
        return _manager;
    }
    /// <summary>
    /// 私有的构造函数,防止从外部实例化
    /// </summary>
    private MessageHandler()
    {
        cachingConnectionFactory = (CachingConnectionFactory) springContextUtils.getBean(CachingConnectionFactory.class);
    }
    /// <summary>
    /// 得到该实例
    /// </summary>
    public static MessageHandler This()
    {
        return Instance();
    }

    private CachingConnectionFactory cachingConnectionFactory;

    private final Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * MQ接口的消息处理中心
     * **/
    public void MqttProcessor(String topic,String content) {
//        if(cachingConnectionFactory.isPublisherReturns()){
         //   logger.info("Sending MQ message = {}",content);
            RabbitMQSender sender = RabbitMQFactory.createSender(cachingConnectionFactory,"amq.topic", "and.andon." + topic);
            sender.send(content);
            logger.info("Sending MQ message = {}",content);
        }
//    }
}
